package com.gukun.springboot.tdengine.service.backuprecover;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gukun.springboot.tdengine.dao.BackupRecoverLogMapper;
import com.gukun.springboot.tdengine.domain.backuprecover.BackupRecoverLogEntity;
import com.gukun.springboot.tdengine.domain.backuprecover.BackupRecoverTaskEntity;
import com.gukun.springboot.tdengine.domain.common.CommonResult;
import com.gukun.springboot.tdengine.service.backuprecover.backup.BackupExecutor;
import com.gukun.springboot.tdengine.service.backuprecover.recover.RecoverExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zhang
 */
@Service
@Slf4j
public class BackupRecoverLogService {
    @Autowired
    private BackupRecoverLogMapper backupRecoverLogMapper;
    @Autowired
    @Lazy
    private BackupRecoverTaskService backupRecoverTaskService;
    @Autowired
    private BackupExecutor backupExecutor;
    @Autowired
    private RecoverExecutor recoverExecutor;

    public void saveOrUpdate(BackupRecoverLogEntity backupRecoverLogEntity) {
        String id = backupRecoverLogEntity.getId();
        if(id == null){
            backupRecoverLogMapper.insert(backupRecoverLogEntity);
        }else{
            backupRecoverLogMapper.updateById(backupRecoverLogEntity);
        }
    }

    public Page<BackupRecoverLogEntity> getLogByTaskId(BackupRecoverLogEntity logEntity) {
        String taskId = logEntity.getTaskId();
        BackupRecoverTaskEntity backupRecoverTaskEntity = backupRecoverTaskService.getById(taskId);
        int current = logEntity.getCurrent();
        int size = logEntity.getSize();
        Page<BackupRecoverLogEntity> page = new Page<>(current,size);
        if(backupRecoverTaskEntity == null){
            return page;
        }
        LambdaQueryWrapper<BackupRecoverLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BackupRecoverLogEntity::getTaskId, taskId);
        queryWrapper.orderByDesc(BackupRecoverLogEntity::getStartTime);
        this.backupRecoverLogMapper.selectPage(page,queryWrapper);
        List<BackupRecoverLogEntity> result = page.getRecords();
        for(BackupRecoverLogEntity log:result){
            setProgress(backupRecoverTaskEntity, log);
        }
        return page;
    }

    private void setProgress(BackupRecoverTaskEntity backupRecoverTaskEntity, BackupRecoverLogEntity log) {
        if(log.getStatus() == 2){
            if(backupRecoverTaskEntity.getOperateType() == 1 && backupRecoverTaskEntity.getOperateTarget() == 2){
                log.setProgress(backupExecutor.getTableBackupProgress());
            }else if(backupRecoverTaskEntity.getOperateType() == 2 && backupRecoverTaskEntity.getOperateTarget() == 2){
                log.setProgress(recoverExecutor.getTableRecoverProgress());
            }else if(backupRecoverTaskEntity.getOperateType() == 1 && backupRecoverTaskEntity.getOperateTarget() == 1){
                log.setProgress(backupExecutor.getTableDataBackupProgress());
            }else{
                log.setProgress(recoverExecutor.getTableDataRecoverProgress());
            }
        }else{
            log.setProgress(0);
        }
    }

    public void deleteLogByTaskId(String id) {
        LambdaQueryWrapper<BackupRecoverLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BackupRecoverLogEntity::getTaskId, id);
        this.backupRecoverLogMapper.delete(queryWrapper);
    }

    public BackupRecoverLogEntity getById(String id) {
        return this.backupRecoverLogMapper.selectById(id);
    }

    /**
     * 获取正在运行的任务日志
     * @param id 日志id
     * @return 日志信息
     */
    public BackupRecoverLogEntity getRunningLogByTaskId(String id) {
        LambdaQueryWrapper<BackupRecoverLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BackupRecoverLogEntity::getId, id);
        BackupRecoverLogEntity result = this.backupRecoverLogMapper.selectOne(queryWrapper);
        String taskId = result.getTaskId();
        BackupRecoverTaskEntity backupRecoverTaskEntity = backupRecoverTaskService.getById(taskId);
        setProgress(backupRecoverTaskEntity, result);
        return result;
    }


}
